#coding:utf-8
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from lovewith.account.views.modules.noticeapi import NoticeApi
from lovewith.views import site_info



#通知
@login_required
def show_all_message(request):
    notice_id = request.GET.get('nid')
    notice = NoticeApi()
    if not notice_id:
        notice_type = request.GET.get('t') or 'all'
        if notice_type == 'unread':
            notice_data = notice.get_by_page(request.user.id, page_no=1, notice_type='unread')
        else:
            notice_data = notice.get_by_page(request.user.id, page_no=1)

        total_count = notice.get_count_by_user(request.user.id)

        return render_to_response('user/notice-list.jinja', {
            'page_size': notice.page_size,
            'total_count': total_count,
            'notice_data': notice_data,
            'notice_type': notice_type
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        total_count = notice.get_count_by_user(request.user.id)
        notice_data = NoticeApi.get_user_notice_by_id(notice_id, request.user.id)

        if not notice_data:
            return HttpResponseRedirect('/u/message/')

        return render_to_response('user/notice-detail.jinja', {
            'total_count': total_count,
            'notice_data': notice_data
        }, context_instance=RequestContext(request, processors=[site_info]))